//
//  SPManageModel.swift
//  SparklingPets
//
//  Created by iOS-Czz on 2023/10/26.
//

import UIKit
class SPManageViewMode{
    var dataSource = [SPManageModel]()
    var lately = false
}
extension SPManageViewMode{
    func getPageData(_ searchStr:String, onSuccess: ((_ models: [SPManageModel]) ->Void)?){
        let dataHelper = SPManageModel.getUsingLKDBHelper()
        var sql = "select * from @t"
        if !searchStr.isEmpty {
            sql = "select * from @t where \(searchStr)"
        }
        let modelArr = dataHelper.search(withSQL: sql, to: SPManageModel.self) as! [SPManageModel]
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let sortedModels = modelArr.sorted { (model1, model2) -> Bool in
            guard let date1 = dateFormatter.date(from: model1.date),
                  let date2 = dateFormatter.date(from: model2.date) else {
                return false // 如果转换失败则直接返回 false
            }
            return date1 > date2
        }
        self.dataSource = sortedModels
        onSuccess?(self.dataSource)
    }
}

@objcMembers
class SPManageModel: STBaseModel,Codable {
    var name = ""
    var manage = ""
    var date = ""
    var time = ""
    var rator = ""
    var isCompleted = false
    var isDefault = true
    override class func getTableName() -> String {
        return "BreedingCostTab"
    }
}
